package common.util;

import com.alibaba.fastjson.JSON;
import com.yuntsg.ruiijn.database.mapper.UnitBasicMapper;
import common.proxy.HttpUtil;
import com.yuntsg.nnsfcp.common.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.impl.client.BasicCookieStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("index")
@Slf4j
@SuppressWarnings("ALL")
public class WOS_GET_PDF_UTIL {
    @Autowired
    UnitBasicMapper unitBasicMapper;

    @Resource
    RedisUtil redisUtil;

    public static void main(String[] args) {
        String abase64 = "";
        String newLink = "";
        String url = "";


        abase64 = XXTEA.encryptToBase64StringNoSafe("A1986A589300017", "R0HWlCI3ndd1s8S3");
        url = "https://www.pubtsg.com/api/getpdfurl.do?unitid=1&type=1&query=" + abase64;

        BasicCookieStore cookieStore = new BasicCookieStore();
        String html = HttpUtil.postHtmlQuanWen(cookieStore, url, "", "");
        System.out.println(html);
        Map<String, Object> map = new HashMap<String, Object>();
        map = JSON.parseObject(html, Map.class);
        if (map.get("pdfurl") != null) {//1094827  //40
            String pdfUrl = (String) map.get("pdfurl");
            String getpdfurl = getpdfurl(pdfUrl, 1094827, 40);
            System.out.println(getpdfurl);
        }

    }

    public static String getpdfurl(String pdfPath, Integer uid, Integer gid) {
        String[] prevEncryptStrArray = pdfPath.split(",");
        String newPaths = "";
        for (String str : prevEncryptStrArray) {
            String[] array = str.split("\\|");
            String fileName = "show";
            if (array.length > 3) {
                //给str赋值去掉文件名的待加密字符串
                str = str.replace("|" + array[3], "");
                fileName = array[3].replace("#_#", ".");
            }
            String encryptStrPre = uid + "|" + gid + "|" + str + "|" + System.currentTimeMillis();
            String encryptStr = XXTEASafe.encryptToBase64String(encryptStrPre, "VJpHo2naZ9");
            String suffix = ".pdf";
            if (str.contains("rar")) {
                suffix = ".rar";
            }
            String encoderFileName = URLEncoder.encode(fileName).replace("+", "%20");
            String newLink = "http://attach.pubtsg.com:8088/attach/" + encoderFileName + "?query=" + encryptStr + "&view=true&type=" + suffix;
            newPaths = newPaths + "|" + newLink;
        }
        if (newPaths.length() > 1) {
            newPaths = newPaths.substring(1);
        }
        return newPaths;
    }

    @RequestMapping("/")
    public ModelAndView index() {
        return new ModelAndView("index");
    }


}
